package com.finace.money.ui.activity;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.finace.money.App;
import com.finace.money.R;
import com.finace.money.common.Constant;
import com.finace.money.common.util.StatusBarUtil;
import com.finace.money.common.util.UIUtil;
import com.finace.money.di.component.AppComponent;
import com.finace.money.presenter.BasePresenter;
import com.finace.money.ui.widget.BaseView;
import com.finace.money.ui.widget.TouchInterceptLayout;
import com.tbruyelle.rxpermissions2.RxPermissions;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.functions.Consumer;

/**
 * Created by ZHP on 2017/6/24.
 */

public abstract class BaseActivity<T extends BasePresenter> extends AppCompatActivity implements BaseView {


    private Unbinder mUnbinder;

    protected App mApplication;

    private FrameLayout mRootView;

    private FrameLayout mViewContent;

    private TouchInterceptLayout mProgress;

    private ImageView loading;

    private boolean showLoading = true;
    protected boolean onResume = true;
    public static final int PLUGIN_VALID = 0;
    public static final int PLUGIN_NOT_INSTALLED = -1;
    public static final int PLUGIN_NEED_UPGRADE = 2;
    @Inject
    public
    T mPresenter;
    private AnimationDrawable loadDraw;
    private BroadcastReceiver mBroadcastReceiver;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(setLayoutView());
        mUnbinder = ButterKnife.bind(this);
        this.mApplication = (App) getApplication();
        setupAcitivtyComponent(mApplication.getAppComponent());
        //状态栏透明
        StatusBarUtil.transparencyBar(this);
        //设置状态栏文字颜色及图标为深色
//        StatusBarUtil.StatusBarLightMode(this);
        init();

    }

    private View setLayoutView() {

        mRootView = (FrameLayout) View.inflate(this, R.layout.base_progress, null);

        mViewContent = (FrameLayout) mRootView.findViewById(R.id.view_content);

        mProgress = (TouchInterceptLayout) mRootView.findViewById(R.id.progress);

        loading = mRootView.findViewById(R.id.loading);

        loading.setImageResource(R.drawable.loading);
        loadDraw = (AnimationDrawable) loading.getDrawable();

        setRealContentView();
        return mRootView;
    }

    private void setRealContentView() {

        View realContentView = LayoutInflater.from(this).inflate(setLayout(), mViewContent, true);
        mUnbinder = ButterKnife.bind(this, realContentView);
    }

    /**
     * 申请SD卡写入权限
     * @param consumer 权限结果回调
     */
    @RequiresApi(api = Build.VERSION_CODES.M)
    protected void requestSdWritePermission(Consumer<Boolean> consumer){
        // 检查权限
        RxPermissions.getInstance(this)
                .request(Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.CAMERA,
                        Manifest.permission.ACCESS_NETWORK_STATE,
                        Manifest.permission.CHANGE_WIFI_STATE,
                        Manifest.permission.CHANGE_NETWORK_STATE)
                .subscribe(consumer);
        checkPermission();
    }

    /**
     * write_setting权限申请，必须这么写
     */
    @RequiresApi(api = Build.VERSION_CODES.M)
    private void checkPermission() {
        if (!Settings.System.canWrite(this)) {
            UIUtil.showToastSafe(getString(R.string.permission_tip));
            Uri selfPackageUri = Uri.parse("package:"
                    + getPackageName());
            Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS,
                    selfPackageUri);
            startActivity(intent);
        }
    }

    public void unregisterReceiver(BroadcastReceiver broadcastReceiver){
        mBroadcastReceiver  = broadcastReceiver;
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mUnbinder != Unbinder.EMPTY) {
            mUnbinder.unbind();
        }
        if(null != mBroadcastReceiver){
            LocalBroadcastManager.getInstance(this).unregisterReceiver(mBroadcastReceiver);
        }
    }

    public abstract int setLayout();


    public abstract void init();

    @Override
    public void showLoading() {
        if (showLoading) {
            mProgress.setVisibility(View.VISIBLE);
            mViewContent.setEnabled(false);
            loadDraw.start();
        }
    }

    public void showLoading(boolean showLoading) {
        this.showLoading = showLoading;
        if(showLoading){
            mProgress.setVisibility(View.VISIBLE);
            mViewContent.setEnabled(false);
            loadDraw.start();
        }else {
            mProgress.setVisibility(View.GONE);
            mViewContent.setEnabled(true);
            loadDraw.stop();
        }
    }


    @Override
    public void showError(String msg) {
        mProgress.setVisibility(View.GONE);
        loadDraw.stop();
        mViewContent.setEnabled(true);
        UIUtil.showToastSafe(msg);
    }

    @Override
    public void dismissLoading() {
        mProgress.setVisibility(View.GONE);
        mViewContent.setEnabled(true);
        loadDraw.stop();
    }

    public abstract void setupAcitivtyComponent(AppComponent appComponent);

    public boolean isOnResume() {
        return onResume;
    }

    public void goHome(int position) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra(Constant.main,position);
        startActivity(intent);
        finish();
    }

    /*****************************************************************
     * mMode参数解释： "00" - 启动银联正式环境 "01" - 连接银联测试环境
     *****************************************************************/
}
